package com.ksoft.gfx;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ksoft.utils.Pkg;

public class MainActivity extends ListActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final String FORM_PKG = "com.ksoft.gfx.items";
		List<Class<?>> list = Pkg.InflateClasses(this, Package.getPackage(FORM_PKG));
		setListAdapter(new ClassList(this, list));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Class<?> type = (Class<?>) l.getAdapter().getItem(position);
		Intent intent = new Intent(this, type);
		startActivity(intent);
	}

}

class ClassList extends BaseAdapter{
	private Context context;
	private List<Class<?>> data;
	
	public ClassList(Context context, List<Class<?>> data){
		this.context = context;
		this.data = data;
	}
	
	@Override
	public int getCount() { return data.size(); }

	@Override
	public Object getItem(int pos) { return  data.get(pos); }

	@Override
	public long getItemId(int arg0) { return 0;	}

	@Override
	public View getView(int pos, View view, ViewGroup arg2) {
		if (view == null)
			view = View.inflate(context, android.R.layout.simple_list_item_1, null);
		
		Class<?> type = (Class<?>) getItem(pos);
		
		((TextView)view.findViewById(android.R.id.text1)).setText(type.getSimpleName());
		
		return view;
	}
	
}
